package pers.lws.erp;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author liuws
 * @create 2020-09-03 16:00
 */
public class CodeGenerator {

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("liuws");
        gc.setOpen(false);
        gc.setFileOverride(false); //是否覆盖
        //gc.setServiceName("$sService"); //去service的I前缀
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
        //gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.115.21.248:3306/db_erp?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("lws666");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("code");
        pc.setParent("pers.lws.erp");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setXml("mapper.mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("production_plan","production_plan_product");//设置要映射表
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //乐观锁
        strategy.setVersionFieldName("version");

        //自动填充配置
        TableFill gmt_create_time = new TableFill("create_time", FieldFill.INSERT);
        TableFill gmt_update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        //tableFill集合
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmt_create_time);
        tableFills.add(gmt_update_time);
        //设置
        strategy.setTableFillList(tableFills);

        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
